#pragma

#include <iostream>
#include <string>
#include <queue>
#include <pthread.h>
#include "Cond.hpp"
#include "Mutex.hpp"

const static uint32_t gcap = 5;
template <class T>
class BlockQueue
{
private:
    bool IsFull()
    {
        return _bq.size()>=_cap;
    }
    bool IsEmpty()
    {
        return _bq.empty();
    }
public:
    BlockQueue(uint32_t cap=gcap):_cap(cap),_c_wait_num(0),_p_wait_num(0)
    {
    }
    void Enqueue(const T &in)
    {
        {
            LockGuard lockguard(&_lock);
            while(IsFull())
            {
                _p_wait_num++;
                _p_cond.Wait(_lock);
                _p_wait_num--;
            }
            //不满的
            _bq.push(in);
            if(_c_wait_num>0)
                _c_cond.NotifyOne();
        }
    }
    void Pop(T *out)
    {
         {
            LockGuard lockguard(&_lock);
            while (IsEmpty())
            {
                _c_wait_num++;
                _c_cond.Wait(_lock);
                _c_wait_num--;
            }

            *out = _bq.front();
            _bq.pop();
            if (_p_wait_num > 0)
                _p_cond.NotifyOne();
        }
    }
    ~BlockQueue()
    {
    }

private:
    std::queue<T> _bq;
    uint32_t _cap;

    Mutex _lock;
    Cond _c_cond;// 消费者用的cond
    Cond _p_cond;// 生产者用的cond

    int _c_wait_num;// 当前消费者等待的个数
    int _p_wait_num;// 当前生产者等待的个数
};